<%@page import="constants.TasksConstants"%>
<%@page import="constants.AttributeConstants"%>
<%@page import="org.apache.commons.io.filefilter.OrFileFilter"%>
<%@page import="servlets.Order"%>
<%@page import="myJDBC.entities.Orders"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="ISO-8859-1"%>
<%@page import="myJDBC.lowLevel.*"%>
<%@page import="myJDBC.lowLevel.Objects"%>
<%@ page import="java.util.*"%>
<%@ page import="constants.ObjectConstants"%>
<%@ page import="constants.ObjectTypeConstants"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style>
<%@include file="smallJsps/styles.jsp" %>
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<%@include file="smallJsps/bootstrapInclude.jsp" %>
</head>
<body>	
	<%@include file="smallJsps/logoutButton.jsp" %>
	<%
		final int  adminid=ObjectConstants.admin;//id admin object
			ArrayList<Integer> myGroups = (((IRemoteLoginnedUserBean) request.getSession().getAttribute("userBean")).getGroupIds());
		 	boolean admin= false;
		for(int q: myGroups){
			if(q==adminid)
				admin=true;
		}
	%>
	
	<div class="row-fluid">
		<div class="span6 offset2">
	<div class="tabbable"> <!-- Only required for left/right tabs -->
	  <ul class="nav nav-tabs">
	    <li><a href="#tab1" data-toggle="tab">take order</a></li>
	    <li class="active"><a href="#tab2" data-toggle="tab">work with order</a></li>
	    <li><a href="#tab3" data-toggle="tab">explorer</a></li>
	    <% if(admin){//Ð°Ð¹Ð´Ð¸ Ð¾Ð±ÑÐµÐºÑÐ° Ð°Ð´Ð¼Ð¸Ð½Ð° %>
	    <li><a href="#tab4" data-toggle="tab">create</a></li>
	    <li><a href="#tab5" data-toggle="tab">main menu</a></li>
	    <%} %>
	  </ul>
	  <div class="tab-content">
	    <div class="tab-pane" id="tab1">
	      <h3>Take task:</h3>
		      	<table class="table table-striped table-condensed" border="3">	
		      		<tr><th>type task</th><th>client</th><th>adress</th></tr>
		    		<% {
		      			ArrayList<ArrayList<String>> freeTasks= new ArrayList<ArrayList<String>>();
		      			freeTasks=Orders.retFreeManualTasksWithGroups(myGroups);
		      			for(ArrayList<String> mtask: freeTasks){
		      		%>		      				      		
					<tr>
					<td><a href="Order?TaskId=<%=mtask.get(0)%>&type=takeTask"><%=mtask.get(1)%></a></td>
					<td><a href="Order?TaskId=<%=mtask.get(0)%>&type=takeTask"><%=Orders.retTaskCustomerFIO(Integer.parseInt(mtask.get(0)))%></a></td>
					<td><a href="Order?TaskId=<%=mtask.get(0)%>&type=takeTask"><%=Orders.retTaskCustomerAdress(Integer.parseInt(mtask.get(0)))%></a></td>
					</tr>
			    	<%}}%>
			    </table>	
	    </div>
	    <div class="tab-pane active" id="tab2">
	       <h3>work with task:</h3>
	      		<form method="get" action="privateOffice">
		      	<select name="workinTaskId" onchange="submit()">
		   		<% 
		   			int selectedWorkTask=-1;
		   			if(request.getParameter("workinTaskId")!=null)
		   				selectedWorkTask=Integer.parseInt(request.getParameter("workinTaskId"));
		      		ArrayList<ArrayList<String>> myTasks= new ArrayList<ArrayList<String>>();
		      		int userId=((IRemoteLoginnedUserBean) request.getSession().getAttribute("userBean")).getObjectId();
		      		myTasks=Orders.retMyManualTasks(userId);
		      		for(ArrayList<String> mtask: myTasks){
					   	out.print("<option ");
						if(Integer.parseInt(mtask.get(0))==selectedWorkTask)
							 out.print("selected");
						out.println(" value="+mtask.get(0)+">"+mtask.get(1)+" for "+Orders.retTaskCustomerFIO(Integer.parseInt(mtask.get(0)))+"</option>");
		      		}
		      	if(myTasks.size()!=0){
		      		if(selectedWorkTask==-1)
		      			selectedWorkTask=Integer.parseInt(myTasks.get(0).get(0));
					ArrayList<String> ordInfo=new ArrayList<String>();
					ordInfo=Orders.retOrderInfo(selectedWorkTask);
					int groupId=Integer.parseInt(ordInfo.get(0));
		      	%>
		      	</select>
		      </form>
		      	<br><address>order id: <%=ordInfo.get(1)%>; group:<%=References.returnNameObj(AttributeConstants.ref_group_id, selectedWorkTask).get(1)%>
		      	<br>description: <%=ordInfo.get(2)%>
		      	<br>fio: <%=ordInfo.get(3)%>
		      	<br>country: <%=ordInfo.get(4)%>
		      	<br>city: <%=ordInfo.get(5)%>
		      	<br>district: <%=ordInfo.get(6)%>
		      	<br>street: <%=ordInfo.get(7)%>
		      	<br>house: <%=ordInfo.get(8)%>
		      	<br>porch: <%=ordInfo.get(9)%>
		      	<br>appart: <%=ordInfo.get(10)%></address>
		      	<% 
		      		if((Objects.returnObjInfo(selectedWorkTask).get(0)).equals(TasksConstants.put_line_task)){
		      			%><%@include file="entities/put_line_task.jsp" %><%
                    }else if((Objects.returnObjInfo(selectedWorkTask).get(0)).equals(TasksConstants.setup_cart_task)){
		      			%><%@include file="entities/setup_cart_task.jsp" %><%
                    }else if((Objects.returnObjInfo(selectedWorkTask).get(0)).equals(TasksConstants.setup_router_task)){
		      			%><%@include file="entities/setup_router_task.jsp" %><%
		      		}
		      	}else
		      	out.print("</select></form>");
		      	%>
	    </div>
	    <div class="tab-pane" id="tab3">
	     	<%
	     	int objId=57;
	     	int objTypeId=Integer.parseInt(Objects.returnObjectTypeId(objId));
			ArrayList<String> listWithAttributes2 = ObjectTypes.returnObjectType(objTypeId);
			int barier = listWithAttributes2.indexOf(null);		
			ArrayList<String> alnamedesc= new ArrayList<String>();
			alnamedesc=Objects.returnObjInfo(objId);
			ArrayList<String> parinf= new ArrayList<String>();
			if(!alnamedesc.get(3).equals("0"))
				parinf=Objects.returnObjInfo(Integer.parseInt(alnamedesc.get(3)));
			%>
			<h3>Object info</h3>
			<table class="table table-striped table-condensed" border="3">
				<tr><td><b>name</b></td><td><%=alnamedesc.get(0)%></td></tr>
				<tr><td><b>decription</b></td><td><%=alnamedesc.get(1)%></td></tr>
				<tr><td><b>object type</b></td><td><%=alnamedesc.get(2)%></td></tr>
				<%if(parinf.size()!=0){ %>
				<tr><td><b>parent</b></td><td><a href="workinJsp?objId=<%=alnamedesc.get(3)%>"><% 
				out.print(parinf.get(2)+" || "+parinf.get(0)+" || "+parinf.get(1)); 
				}		%></a></td></tr>
			</table>
			<h3>Parametres</h3>
			<table class="table table-striped table-condensed" border="3">
				<tr>
					<th>name</th>
					<th>value</th>
				</tr>
				<%
					ArrayList<ArrayList<String>> params = new ArrayList<ArrayList<String>>();
					params= Params.retParams(objId);//varch list date val
					for(int i=0;i<params.get(0).size(); i+=2){//id value   
						int attrId=Integer.parseInt(params.get(0).get(i));
						out.print("<tr><td>" + Attributes.returnAttribute(attrId).get(1));
						out.print("</td><td>" + Params.returnVarchNumb(attrId, objId));	
					}
					for(int i=0;i<params.get(1).size(); i+=2){//id value   
						int attrId=Integer.parseInt(params.get(1).get(i));
						out.print("<tr><td>" + Attributes.returnAttribute(attrId).get(1));
						out.print("</td><td>" + Params.returnArrValue(attrId, objId));	
					}
					for(int i=0;i<params.get(2).size(); i+=2){//id value   
						int attrId=Integer.parseInt(params.get(2).get(i));
						out.print("<tr><td>" + Attributes.returnAttribute(attrId).get(1));
						out.print("</td><td>" + Params.returnDate(attrId, objId));	
					}
					for(int i=0;i<params.get(3).size(); i+=2){//id value   
						int attrId=Integer.parseInt(params.get(3).get(i));
						out.print("<tr><td>" + Attributes.returnAttribute(attrId).get(1));
						out.print("</td><td>" + Params.returnDate(attrId, objId));	
					}
					//refs
					ArrayList<Integer> refs= new ArrayList<Integer>();
					refs= References.retRefereces(objId);
					for(int i=0;i<refs.size(); i+=2){//id value   
						int attrId=refs.get(i);
						out.print("<tr><td>" + Attributes.returnAttribute(attrId).get(1));
						out.print("</td><td><a href=\"workinJsp?objId="+ refs.get(i+1)+"\">" + Objects.returnObjInfo(refs.get(i+1)).get(0)+"</a>");
					}
					out.print("</td></tr>");
					
				%>
			</table>
			<%
				ArrayList<Integer> child= new ArrayList<Integer>();
				child=Objects.returnChildrens(objId);
				if(child.size()!=0){
			%>
			<h3>Childs</h3>
			<table class="table table-striped table-condensed" border="3">
				<tr>
					<th>name</th>
					<th>descr</th>
					<th>obj type</th>
				</tr>
			<%
				ArrayList<String> onechild= new ArrayList<String>();
				for(Integer chId:child){
					onechild=Objects.returnObjInfo(chId);
					//<a href=\"workinJsp?objId="+onechild.get(3)+">>" + onechild.get(1)+ "</a><tr><td><a href=\"workinJsp?objId="+onechild.get(3)+">>" + onechild.get(2)+"</a></td></tr>
					out.print("<tr><td><a href=\"workinJsp?objId="+ Integer.toString(chId)+"\">" + onechild.get(0)+ "</a></td>");
					//out.print("<td><a href=\"workinJsp?objId="+ Integer.toString(chId)+"\">" + onechild.get(1)+ "</a></td>");
					//out.print("<td><a href=\"workinJsp?objId="+ Integer.toString(chId)+"\">" + onechild.get(2)+ "</a></td></tr>");
					out.print("<td>" + onechild.get(1)+ "</td>");
					out.print("<td>" + onechild.get(2)+ "</td></tr>");
				}
				out.print("</table>");
				}
			 %>
			 <% 
				ArrayList<Integer> ref = new ArrayList<Integer>();
				ref=References.returnLinksOnObj(objId);
				if(ref.size()!=0){
			 %>
			<h3>Links to this object</h3>
			<table class="table table-striped table-condensed" border="3">
				<tr>
					<th>name</th>
					<th>descr</th>
					<th>obj type</th>
				</tr>
				<%
					for(Integer chId:ref){
					ArrayList<String> onechild= new ArrayList<String>();
					onechild=Objects.returnObjInfo(chId);
					//<a href=\"workinJsp?objId="+onechild.get(3)+">>" + onechild.get(1)+ "</a><tr><td><a href=\"workinJsp?objId="+onechild.get(3)+">>" + onechild.get(2)+"</a></td></tr>
					out.print("<tr><td><a href=\"workinJsp?objId="+ Integer.toString(chId)+"\">" + onechild.get(0)+ "</a></td>");
					//out.print("<td><a href=\"workinJsp?objId="+ Integer.toString(chId)+"\">" + onechild.get(1)+ "</a></td>");
					//out.print("<td><a href=\"workinJsp?objId="+ Integer.toString(chId)+"\">" + onechild.get(2)+ "</a></td></tr>");
					out.print("<td>" + onechild.get(1)+ "</td>");
					out.print("<td>" + onechild.get(2)+ "</td></tr>");
				}
				%>
			</table>
			<% 
			}
			%>	
			<a class="btn btn-large" href="createObject2?sel=<%=ObjectTypeConstants.city%>&par=<%=objId%>">create city</a>	 
    	</div>
    	<div class="tab-pane" id="tab4">
    		<a class="btn" href="createCustomer">create order</a>
    		<a class="btn" href="createObject2?sel=<%=ObjectTypeConstants.user%>">create user</a>    		
	  	</div>
    	<div class="tab-pane" id="tab5">
    		<%@include file="mainMenu.jsp" %>
    	</div>	  	
	  </div>
	</div>		
	</div>
	</div>
</body>
</html>